function Player(data){
	this.id = data.id;
	this.name = data.name;
	this.selectedCardTalonId = 0;
	this.endAttack = false;
	this.iAcceptCards = false;
	this.iEndAttack = false;
}

Player.prototype.cards = function(){
	table.addTracerInfo(new Info({'description':'getting player'+this.id+" cards on hand",'triggered':'cards()'}));
	return game.talonCards.getCardsbyPlayerId(this.id);
	table.addTracerInfo(new Info({'description':'getting player'+this.id+" cards on hand[DONE]",'triggered':'cards()'}));
}

Player.prototype.isOtherPlayerEndItsAttack = function(){
	if(this.id==1){
		//check player 2
		return game.players[1].iEndAttack;
	}else{
		//check player 1
		return game.players[0].iEndAttack;
	}
}

Player.prototype.isDefender = function(){
	var val = table.whosDefenderId;
	console.log("isDefender "+val+" = "+this.id);
	if(val==this.id){
		return true;
	}
	return false;
}

Player.prototype.attackToBecomeAttackFromDefender = function(){
	if(this.isDefender()){
		if(this.isOtherPlayerEndItsAttack()){
			// remove all cards on reavealed table.
			game.removeAllRevealedCards();
			table.updateGameInformation();
			game.replenishPlayers();
//			table.reLoadPlayersTable(1);
//			table.reLoadPlayersTable(2);
			if(this.id==table.whosDefenderId){
				var defender = table.whosDefenderId;
				table.whosDefenderId = table.whosAttackerId;
				table.whosAttackerId = defender
				table.whosTurn = table.whosAttackerId;
				
				game.players[0].iAcceptCards = false;
				game.players[1].iAcceptCards = false;
				
				game.players[0].iEndAttack = false;
				game.players[1].iEndAttack = false;
				table.updateGameInformation();
			}else{//this player is attacker
				// attacker can never become attacker again.
			}
			
			revealedTable.html('');// mandatory direct command
			table.updatePlayerPrivileges();
		}else{
			alert('you cant do that..! the other player did not end its attack!');
		}
	}else{// i am attacker
		alert('Please choose a card to attack');
	}
}

Player.prototype.didOtherPlayerAcceptCards = function(){
	if(this.id==1){
		//check player 2
		return game.players[1].iAcceptCards;
	}else{
		return game.players[0].iAcceptCards;
	}
}

Player.prototype.didOtherPlayerEndAttack = function(){
	if(this.id==1){
		return game.players[1].iEndAttack;
	}else{
		return game.players[0].iEndAttack;
	}
}

Player.prototype.attack = function(talonCardId){
	if(this.isDefender()){
		this.defend(talonCardId);
		return;
	}
	
	
	
	if(this.iEndAttack){
		alert('You end your attack! please wait for the other player to decide!');
		return;
	}
	
	if(this.didOtherPlayerEndAttack()){
		alert('You cant use this card! please accept cards or  attack');
		return;
	}
	
	if(table.whosTurn!=this.id){
		if(!this.didOtherPlayerAcceptCards()){
			alert('its not yout turn please wait....!');
			console.log('its not yout turn please wait....!');
			return;
		}
	}
	
	if(this.iAcceptCards==true){
		alert('you cant attack until the other player did not end its attack');
		return;
	}
	
	if(!this.checkCard(talonCardId)){
		return;
	}
	
	var isTrue = this.didOtherPlayerAcceptCards();
	if(!isTrue){
		table.whosNextTurn();
	}
	
	table.addTracerInfo(new Info({'description':'player'+this.id+" Attacks ",'triggered':'attack('+talonCardId+')'}));

	var attackPlayerUrl = "index.php?/player/attack/"+this.id+"/"+talonCardId;
	var cardsRevealed = this.cardsRevealed;
	var playerId = this.id;
	var playerCards = this.cards();
	$.ajax({
		url: attackPlayerUrl,
		dataType: 'json',
		async:false,
		success: function(turnCard) { // should return the last card inserted to turn table
			if($.isEmptyObject(turnCard)){
				return;
			}
			
			var card = new Turn(turnCard[0]);
			console.log(talonCardId+" = "+card.talon_id);
				game.talonCards.updateTalonCardRevealed(talonCardId);
			
			game.cardsRevealed.push(card);
			game.lastCardRevealed = card;
			table.appendToRevealedTable(card,playerId);
			if(playerCards.length==0){
				game.replenishPlayer(playerId);
			}
		}
	}).done(function(){
		table.addTracerInfo(new Info({'description':'player'+this.id+" Attacks [DONE]",'triggered':'attack('+talonCardId+')'}));
		//console.log('player card remaining is '+this.cards().length);
		table.updateGameInformation();
		if(!isTrue){
			table.updatePlayerPrivileges();
		}
	});
	
	//table.updateGameInformation();
	
}

Player.prototype.checkCard = function(talonCardId){
	if(table.whosTurn!=this.id){
		if(!this.didOtherPlayerAcceptCards()){
			alert('its not yout turn please wait....!');
			console.log('its not yout turn please wait....!');
			return false;
		}
	}
	
	var lastCardRevealed = game.lastCardRevealed;
	var isTrue_ = jQuery.isEmptyObject(lastCardRevealed);
	if(!isTrue_){
		var talonId = lastCardRevealed.talon_id;
		var newtalonCard = game.talonCards.getCardById(talonCardId);
		var oldtalonCard = game.talonCards.getCardById(talonId);
		var cardIdofLastInsertedTurn = game.cards.getCardById(oldtalonCard.card_id);
		var cardIdofNewOne = game.cards.getCardById(newtalonCard.card_id);
		console.log('cardIdofLastInsertedTurn '+cardIdofLastInsertedTurn.id);
		console.log('cardIdofNewOne ='+cardIdofNewOne.id);
	}
	var cardsRevealed = game.cardsRevealed;
	console.log(cardsRevealed.length);
	//check the new selected card 
	// if its face value exsist in revealed table
	console.log(this.id);
	console.log(this.isDefender());
	if(cardsRevealed.length > 1 && !this.isDefender()){
		// check face value
		var isTrue = cardsRevealed.checkFaceValue(newtalonCard);
		if(!isTrue){
			alert('The card suit you selected is not in revealed table');
			return false;
		}
		return true;
	}
	// if this player is an attacker mode.
	// no need to check its card face value..
	if(this.id==table.whosAttackerId){
		console.log('im the attacker i dont need to check card ranking');
		return true;
	}
	
	
	if(Number(cardIdofLastInsertedTurn.id) > Number(cardIdofNewOne.id) || !game.isSuitTheSame(oldtalonCard.card_id, newtalonCard.card_id)){
			if(!game.isTheSameWithMasterSuit(talonCardId, game.masterSuit)){
				alert('Your card has a lower ranking and is not a master suit nor a compatible suit to attacker card! sorry');
				return false;
			}else{
				var con1 = game.isTheSameWithMasterSuit(talonCardId, game.masterSuit);
				var con2 = game.isTheSameWithMasterSuit(oldtalonCard.id, game.masterSuit);
				if(con1 && con2){
					var c1 = game.cards.getCardById(oldtalonCard.card_id);
					var c2 = game.cards.getCardById(newtalonCard.card_id);
					if(Number(c1.id) > Number(c2.id)){
						alert('You Card has a lower ranking!');
						return false;
					}
				}
				return true;
			}
			
			alert('Not the same suit and your card has lower ranking sorry!');
			return false;
	}
	return true;
}

Player.prototype.defend = function(talonCardId){
	console.log('defending my cards');
	
	
	
	if(this.iEndAttack){
		alert('You end your attack! please wait for the other player to decide!');
		return;
	}
	
	if(this.didOtherPlayerEndAttack()){
		alert('You cant use this card! please accept cards or  attack');
		return;
	}
	
	if(table.whosTurn!=this.id){
		if(!this.didOtherPlayerAcceptCards()){
			alert('its not yout turn please wait....!');
			console.log('its not yout turn please wait....!');
			return;
		}
	}
	
	if(this.iAcceptCards==true){
		alert('you cant attack until the other player did not end its attack');
		return;
	}
	
	if(!this.checkCard(talonCardId)){
		return;
	}
	
	var isTrue = this.didOtherPlayerAcceptCards();
	if(!isTrue){
		table.whosNextTurn();
	}
	
	var defendPlayerUrl = "index.php?/player/defend/"+this.id+"/"+talonCardId;
	var playerId = this.id;
	
	$.ajax({
		url: defendPlayerUrl,
		dataType: 'json',
		async:false,
		success: function(turnCard) { // should return the last card inserted to turn table
			if($.isEmptyObject(turnCard)){
				return;
			}
			var card = new Turn(turnCard[0]);
			
				game.talonCards.updateTalonCardRevealed(talonCardId);
			
			game.cardsRevealed.push(card);
			game.lastCardRevealed = card;
			table.appendToRevealedTable(card,playerId);
		}
	}).done(function(){
		//game.reloadUI();
		table.addTracerInfo(new Info({'description':'player'+this.id+" dDEFEND [DONE]",'triggered':'defend('+talonCardId+')'}));
		table.updateGameInformation();
		if(!isTrue){
			table.updatePlayerPrivileges();
		}
	});
	table.updateGameInformation();
	
}
Player.prototype.drawCard = function(){
	var count = game.talonCards.getActiveTalonCards().length;
	console.log('getActiveTalonCards ='+count);
	if(count==0){
		return;
	}
	table.addTracerInfo(new Info({'description':'player'+this.id+" draw a card",'triggered':'drawCard()'}));
	var card = '';
	if(game.id==0){
		alert('No active Game session please create new one.');
		return;
	}
	if(this.cards().length>=6){
		alert('You have already reach limit cards on hands');
		return;
	}
	$.when(game.drawCardTo(this.id)).then(function(e){
		card = e;
		table.addTracerInfo(new Info({'description':'player'+this.id+" draw a card[DONE]",'triggered':'drawCard()'}));	

	});//talon card
	table.updatePlayerPrivileges();
	table.updateGameInformation();
}

Player.prototype.switchTurn = function(){
	if(this.id==1){
		table.whosTurn = 2;
	}else{
		table.whosTurn = 1;
	}
	console.log('table.whosTurn = '+table.whosTurn);
	table.updateGameInformation();
}

Player.prototype.theOtherPlayerId = function(){
	if(this.id==1){
		return 1;
	}else{
		return 0;
	}
}

Player.prototype.acceptCards = function(){
	//alert('accept cards test');
	var  val = table.whosTurn;
	
	if(val!=this.id){
		alert('its not your turn!');
		return;
	}
	
	if(this.iAcceptCards == false){
		console.log('this.endAttack = '+this.endAttack);
		if(this.iEndAttack==true){
			alert('Idiot please wait until other players turn is over!');
			return;
		}
		$('#acceptCardsp'+this.id).hide();
		this.iAcceptCards = true;
		this.switchTurn();
		alert('player'+this.id+' accept the cards waiting for additional cards');
		console.log('player'+this.id+' accept the cards');
		return;
	}else{
		console.log('current turn id: '+this.id);
		this.switchTurn();
	}
	var pId = this.theOtherPlayerId();
	console.log(pId);
	game.players[pId].iEndAttack =false;
	this.iAcceptCards = false;
	//alert('cards accepted by player'+this.id);
	console.log('cards accepted by player'+this.id);
	//return;
	var cardsToBeAccepted = game.cardsRevealed; //values from turn table
	playerId = this.id;
	var talonCards_ = this.cards();
	//$.each(cardsToBeAccepted,function(key,card){
	
		var acceptCardsUrl = "index.php?/player/acceptCards/"+playerId;//+"/"+card.talon_id;
		$.ajax({
			url: acceptCardsUrl, 
			dataType: 'json',
			async:false,
			success: function(talons) {
				$(talons).each(function(key,tCard){
					//game.talonCards.updateCardsRevealed(val.id);
					game.talonCards.updateReplenishee_Id(tCard.id,playerId);
					if(!game.talonCards.exist(tCard.id)){
						game.talonCards.push(new Talon(tCard));
						game.talonCards.updateImg(tCard.card_id,val.id);
					}
					if(!talonCards_.isAlreadyInPlayerTable(tCard.id)){
						console.log('isAlreadyInPlayerTable = true');
						var talonCard__ = game.talonCards.getCardById(tCard.id);
						table.insertNewCardToPlayer(playerId,talonCard__);
					}
				});
				game.cardsRevealed = new Array();
				if(game.cardsRevealed.length==0){
					revealedTable.html('');
				}else{
					alert('opps!!!  revealed cards not empty');
					console.log('opps!!!  revealed cards not empty');
					table.reloadRevealedTableNoAjax();
				}
			}
		}).done(function(){
			table.updateGameInformation();
			table.updatePlayerPrivileges();
		});
	//});
}

//Player.prototype.endAttack = function(){
//	
//	
//	alert('end attack test');
//	return;
//	var endAttackPlayerUrl = "index.php?/player/endAttack/"+this.id;
//	$.ajax({
//		url: endAttackPlayerUrl, 
//		done:function(){
//			/*TODO: after ending an attack.. cards for both player should be replenished
//			 * the the last attacker should be the last one cards will be replenished.
//			 * 
//			 */
//		}
//	}).done(function(){
//		table.updateGameInformation();
//	});
//}

Player.prototype.info = function(){
	console.log('name : '+this.name+" - id:"+this.id);
}
//players = new Array();